package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.entity.SysUserCyyj;
import com.yuncheng.spcyApi.mapper.SysUserCyyjMapper;
import com.yuncheng.spcyApi.service.ISysUserCyyjService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @Description: 系统-用户常用意见库
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SysUserCyyjServiceImpl extends BaseServiceImpl<SysUserCyyjMapper, SysUserCyyj> implements ISysUserCyyjService {

    @Override
    public List<SysUserCyyj> getCyyjList(String pid, String type1) {
        return list(new LambdaQueryWrapper<SysUserCyyj>()
                .eq(SysUserCyyj::getFuserid, pid)
                .eq(SysUserCyyj::getFtype1, type1));
    }

    @Override
    public String getCyyjContent(String pid, String type1) {
        List<SysUserCyyj> list = list(new LambdaQueryWrapper<SysUserCyyj>()
                .eq(SysUserCyyj::getFuserid, pid)
                .eq(SysUserCyyj::getFtype1, type1));
        if (CollectionUtil.isNotEmpty(list)){
            return list.get(0).getFcontent();
        }

        return "";
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCyyj(String pid, String type1, String type2, String content) {

        SysUserCyyj bean = new SysUserCyyj();
        bean.setId(GetUuIdUtils.NotReplaceUuId());
        bean.setFuserid(pid);
        bean.setFtype1(type1);
        bean.setFtype2(type2);
        bean.setFcontent(content);
        save(bean);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCyyj(String pid, String type1, String content) {
        LambdaUpdateWrapper<SysUserCyyj> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysUserCyyj::getFuserid, pid);
        updateWrapper.eq(SysUserCyyj::getFtype1, type1);

        SysUserCyyj updateBean = new SysUserCyyj();
        updateBean.setFcontent(content);

        update(updateBean, updateWrapper);
    }
}
